package pl.diagnoser.server.persistance.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import pl.diagnoser.client.dto.SymptomDTO;
import pl.diagnoser.server.persistance.map.Estimate;
import pl.diagnoser.server.persistance.map.Symptom;
import pl.diagnoser.server.persistance.tools.HibernateUtil;

public class SymptomDao implements Serializable {
        
	private static final long serialVersionUID = 1L;
	
    @SuppressWarnings("unchecked")
	public List<Symptom> getSymptomsForEstimate(Estimate estimate) {
    	Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        List<Symptom> symptom = session.createCriteria(Symptom.class).add(Restrictions.eq("estimate", estimate)).list();
        session.getTransaction().commit();
		session.close();
		
		return symptom;
    }
	
	public Long saveSymptom(Symptom symptom, Session session) {
            Long generatedId = (Long) session.save(symptom);
            return generatedId;
    }
        
    public SymptomDTO createSymptomDto(Symptom symptom) {
            EstimateDao estimateDao = new EstimateDao();
            return new SymptomDTO( estimateDao.createEstimateDTO(symptom.getEstimate()), 
                                           symptom.getSymptomName(), 
                                           symptom.getSymptomConfidence(), 
                                           symptom.getId());
    }
    
    public List<SymptomDTO> createSymptomDTO(List<Symptom> symptoms) {
    	List<SymptomDTO> symptomsDTO = new ArrayList<SymptomDTO>(symptoms.size());
    	for(Symptom sym : symptoms) {
    		symptomsDTO.add(createSymptomDto(sym));
    	}
    	
    	return symptomsDTO;
    }
}
